/*
 * Created by JFormDesigner on Mon Jun 24 15:04:55 CST 2024
 */

package scct.chenlixian.ui;

import scct.chenlixian.dao.UserDao;
import scct.chenlixian.model.USer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author DELL
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
    }

    private void btnLogin(ActionEvent e) {
        System.out.println("按钮点击了");
        //获取文本框的输入用户密码
        String name = txtName.getText();
        String pass = txtPass.getText();
        System.out.println(name + "," + pass);
        //name.matches("正则表达式") 返回true表示成功 返回false表示验证失败
        if (!name.matches("^\\w{3,10}$")) {
//            Swing的消息对话框
            JOptionPane.showMessageDialog(null, "你输入的用户名不合法,请重新输入");
            return;

        }
        if (!pass.matches("^\\w{3,10}$")) {
            JOptionPane.showMessageDialog(null, "你输入的密码不合法,请重新输入");
            return;
        }
        UserDao userDao = new UserDao();
        USer user = userDao.login(name, pass);
        if (user == null) {
            JOptionPane.showMessageDialog(this, "用户名密码错误，请检查登录");
            return;
        } else {
            JOptionPane.showMessageDialog(this, "登录成功");
            FormMain formMain = new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800, 600);
            formMain.setLocationRelativeTo(null);
           //formMain.set
            formMain.setVisible(true);
           //隐藏当前窗体
            this.dispose();

        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - CLX
        label1 = new JLabel();
        label2 = new JLabel();
        txtName = new JTextField();
        label3 = new JLabel();
        txtPass = new JTextField();
        btnLogin = new JButton();
        btnReg = new JButton();

        //======== this ========
        setBackground(new Color(0xccffff));
        setFont(new Font(Font.DIALOG, Font.PLAIN, 22));
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u767b\u5f55");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        contentPane.add(label1);
        label1.setBounds(195, 30, 110, 55);

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        contentPane.add(label2);
        label2.setBounds(90, 135, 55, label2.getPreferredSize().height);
        contentPane.add(txtName);
        txtName.setBounds(150, 100, 190, 30);

        //---- label3 ----
        label3.setText("\u8d26\u53f7");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(90, 100), label3.getPreferredSize()));
        contentPane.add(txtPass);
        txtPass.setBounds(150, 135, 190, 30);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        btnLogin.setBackground(new Color(0x99ffff));
        btnLogin.addActionListener(e -> btnLogin(e));
        contentPane.add(btnLogin);
        btnLogin.setBounds(110, 220, 90, 40);

        //---- btnReg ----
        btnReg.setText("\u6ce8\u518c");
        btnReg.setBackground(new Color(0x99ffff));
        btnReg.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        contentPane.add(btnReg);
        btnReg.setBounds(245, 220, 90, 40);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(525, 394);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - CLX
    private JLabel label1;
    private JLabel label2;
    private JTextField txtName;
    private JLabel label3;
    private JTextField txtPass;
    private JButton btnLogin;
    private JButton btnReg;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
